package com.beiyou.orderingsystemonweb.shop.controller;

import com.beiyou.orderingsystemonweb.shop.domain.ShopFood;
import com.beiyou.orderingsystemonweb.shop.service.ShopFoodService;
import com.beiyou.orderingsystemonweb.shop.service.ShopService;
import com.beiyou.orderingsystemonweb.util.HomeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 餐厅主页:
 */
@Controller
@RequestMapping("/shop")
public class ShopHomeController {
    @RequestMapping("/afterLogin.do")
    public String fistGoHome(HttpServletRequest request, HttpSession session) {
//        return "redirect:/shop/shopHome.do";//跳过安全验证
        //安全登录验证

        String loginMsg = shopService.queryLoginMsg(session.getAttribute("shopId").toString());
        if (loginMsg == null) {
            request.setAttribute("loginMsg", "初次登录，欢迎您");
        } else {
            request.setAttribute("loginMsg", loginMsg);
        }
        return "/shop/loginHistory";


    }

    //好像是地址上有了问题，登录不安全
    @RequestMapping("/saveLoginHistory.data")
    @ResponseBody
    public void SaveLoginHistory(String uid, String key, String pos) {
        if (key == null || uid == null) {
            return;
        }
        shopService.saveShopLoginHistory(uid, key, pos);
    }

    @RequestMapping("/shopHome.do")
    public String shopHome(HttpServletRequest request) {
        HomeUtil.setNavName(request, "shopHome");
        HomeUtil.setUriName(request, "/shop/goShopHome.do?begin=0&size=15");
        return "forward:/shop/homeLeft.do";
    }

    @RequestMapping("/goShopHome.do")
    public String goShopHome(HttpServletRequest request, HttpSession session, Integer begin, Integer size) {
        String sid = (String) session.getAttribute("shopId");
        request.setAttribute("shop", shopService.shopFoodList(sid, begin, size));
        return "/shop/shopfoodlist";
    }

    @RequestMapping("/addFood.do")
    public String addFood(HttpServletRequest request) {
        HomeUtil.setNavName(request, "addFood");
        HomeUtil.setUriName(request, "/shop/goAddFood.do");
        return "forward:/shop/homeLeft.do";
    }

    @RequestMapping("/goAddFood.do")
    public String goAddFood(HttpServletRequest request) {
        request.setAttribute("fileImg", request.getServletContext().getRealPath("/statics/images/food/"));
        return "/shop/shopfood";
    }


    @RequestMapping("/foodManage.do")
    public String foodManage(HttpServletRequest request) {
        HomeUtil.setNavName(request, "foodManage");
        HomeUtil.setUriName(request, "/shop/goFoodManage.do");
        return "forward:/shop/homeLeft.do";
    }

    @RequestMapping("/goFoodManage.do")
    public String goFoodManage(HttpServletRequest request, HttpSession session) {
//shop
        String shopId = session.getAttribute("shopId").toString();
        request.setAttribute("shop", shopService.shopFoodManage(shopId));
        return "/shop/foodmanage";
    }

    @RequestMapping("/shopPerson.do")
    public String shopPerson(HttpServletRequest request) {
        HomeUtil.setNavName(request, "shopPerson");
        HomeUtil.setUriName(request, "/shop/goShopPerson.do");
        return "forward:/shop/homeLeft.do";
    }

    @RequestMapping("/goShopPerson.do")
    public String goShopPerson(HttpServletRequest request, HttpSession session) {
        String shopId = session.getAttribute("shopId").toString();
        request.setAttribute("shop", shopService.queryShopInfo(shopId));
        request.setAttribute("fileImg", request.getServletContext().getRealPath("/statics/images/shop/"));
        return "/shop/shopinfo";
    }


    @RequestMapping("/homeLeft.do")
    public String allRold(HttpServletRequest request, HttpSession session) {
        String sid = (String) session.getAttribute("shopId");
        Map<String, Object> shopInfo = shopService.shopInfo(sid);
        shopInfo.entrySet().forEach(mapEntry -> request.setAttribute(mapEntry.getKey(), mapEntry.getValue()));
        return "/shop/shophome";
    }

    @Autowired
    ShopService shopService;

    @RequestMapping("/updateFoodById.do")
    public String findFoodById(String foodId, HttpSession session, HttpServletRequest request) {
        HomeUtil.setUriName(request, "/shop/goUpdateFoodById.do");
        //当在这里设置uri的时候 foodId也被放在了后面，结果是没有
        System.out.println(request.getAttribute("uri"));
        System.out.println(request.getAttribute("uri"));
        System.out.println(request.getAttribute("uri"));
        System.out.println(request.getAttribute("uri"));

        return "forward:/shop/homeLeft.do";
    }

    @RequestMapping("/goUpdateFoodById.do")
    public String goFoodById(String foodId, HttpServletRequest request) {
        //返回菜品基本信息
        //添加一下标题
        request.setAttribute("isUpdate", "修改菜品");
        request.setAttribute("fileImg", request.getServletContext().getRealPath("/statics/images/food/"));
        request.setAttribute("shop", shopFoodService.queryShopFoodById(foodId));
        return "/shop/shopfood";
    }

    @Autowired
    ShopFoodService shopFoodService;

    //分页查询菜品信息
    @RequestMapping("/queryShopFood.data")
    @ResponseBody
    public Map<String,Object> queryShopFood(String shopId,Integer begin,Integer size,Integer sortType){
        //非空检查
        if (shopId==null||begin==null||size==null){
            return null;
        }
        Map<String,Object> shopListMap = new HashMap();
        shopListMap.put("flag",1);
        shopListMap.put("i",sortType);
        //可能只有店铺  foodNum可以判断有多少
        shopListMap.put("shop",shopService.shopFoodList(shopId, begin-1, size,sortType));
        return  shopListMap;
    }
}
